package com.pin.zheng.xingmulibrary.base;


import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import com.pin.zheng.xingmulibrary.R;


public abstract class BaseFragment extends Fragment {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(this.getLayoutId(), container, false);
        initView(view);
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initTitleLayout(view);
        initConfiguration();
        loadData();
    }

    protected abstract void initTitleLayout(View view);

    /**
     * 初始化配置
     */
    protected abstract void initConfiguration();

    /**
     * 加载数据
     */
    protected abstract void loadData();

    /**
     * 初始化视图
     *
     * @param view
     */
    protected abstract void initView(View view);

    protected abstract int getLayoutId();

//    public void setTitle(View view,String title) {
//        TextView bar_title =  view.findViewById(R.id.bar_title);
//        bar_title.setText(title);
//
//    }
//
//
//    public void setTitle(View view,int r_title) {
//        TextView bar_title =  view.findViewById(R.id.bar_title);
//        bar_title.setText(r_title);
//    }
//
//    public void setTitleRight(View view,String r_title, OnMultiClickListener onClickListener) {
//        TextView bar_title = view.findViewById(R.id.bar_title_right);
//        bar_title.setVisibility(View.VISIBLE);
//        bar_title.setText(r_title);
//        bar_title.setOnClickListener(onClickListener);
//    }
//
//    public void setTitleRight(View view,int r_title, OnMultiClickListener onClickListener) {
//        ImageView bar_title = view.findViewById(R.id.image_right);
//        bar_title.setVisibility(View.VISIBLE);
//        bar_title.setImageResource(r_title);
//        bar_title.setOnClickListener(onClickListener);
//    }
//
//    public void setTitleRight(View view,int r_title) {
//        ImageView bar_title = view.findViewById(R.id.image_right);
//        bar_title.setVisibility(View.VISIBLE);
//        bar_title.setImageResource(r_title);
//    }
//
//    public void setTitleRight(View view,String r_title) {
//        TextView bar_title = view.findViewById(R.id.bar_title_right);
//        bar_title.setVisibility(View.VISIBLE);
//        bar_title.setText(r_title);
//    }


    /**
     * 打开指定activity
     *
     * @param targetClass
     */
    @RequiresApi(api = Build.VERSION_CODES.ECLAIR)
    public void openActivity(Class<?> targetClass) {
        Intent intent = new Intent(getActivity(), targetClass);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.push_left_in,
                R.anim.push_left_out);
    }

    @RequiresApi(api = Build.VERSION_CODES.ECLAIR)
    public void openActivity(Class<?> targetClass, Bundle b) {
        Intent intent = new Intent(getActivity(), targetClass);
        intent.putExtras(b);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.push_left_in,
                R.anim.push_left_out);
    }

    public void startActivityForResult(Class<?> toClass, int requestCode) {
        Intent intent = new Intent(getActivity(), toClass);
        startActivityForResult(intent, requestCode);
        getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    public void startActivityForResult(Class<?> toClass, Bundle b, int requestCode) {
        Intent intent = new Intent(getActivity(), toClass);
        intent.putExtras(b);
        startActivityForResult(intent, requestCode);
        getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }


}
